<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_BASE') or die;

class JFormFieldXmlTypeOrdering extends JFormField
{

    protected $type = 'XmlTypeOrdering';

    protected function getInput()
    {
        $html = array();
        $attr = '';

        $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
        $attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
        $attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';

        // Initialize JavaScript field attributes.
        $attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';

        // Get some field values from the form.
        $iId = (int) $this->form->getValue('id');
        
        $iLibId = (int)  $this->form->getValue('lib_id');
        
        // Build the query for the ordering list.
        $query = 'SELECT ordering AS value, title AS text' .
                ' FROM #__auto_xml_type' .  
                ($iLibId > 0 ? ' WHERE lib_id = ' . $iLibId : '') .
                ' ORDER BY ordering';

        // Create a read-only list (no name) with a hidden input to store the value.
        if ((string) $this->element['readonly'] == 'true')
        {
            $html[] = JHtml::_('list.ordering', '', $query, trim($attr), $this->value, $iId ? 0 : 1);
            $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
        }
        // Create a regular list.
        else
        {
            $html[] = JHtml::_('list.ordering', $this->name, $query, trim($attr), $this->value, $iId ? 0 : 1);
        }

        return implode($html);
    }

}
